/*
 * @Description: 原文挂接
 * @Author: Rfan
 * @Date: 2022-05-04 19:07:54
 * @LastEditTime: 2022-09-08 14:13:47
 */

import type { FC } from 'react';
import { useEffect } from 'react';
import { Button, Form, Input, message, Modal } from 'antd';
import { createExportItem, ICreateExportParams } from '@/api/archives/export';
import { jsonClone } from '@/utils';
import TransferTemplate from '@/components/TransferTemplate';
import type { ICommonDialog } from '@/interfaces/global';
import type { IBaseProp } from '../../index';
import { EnumExportBusType, EnumReceiveTypes } from '@/dictionary/transfer';
import { EnumTemplateTypes } from '@/view/system/TemplateManage/dictionaries';

interface ExportCDDialogProps extends ICommonDialog {
  info: IBaseProp;
  selectItems: number[];
}

const ExportSipDialog: FC<ExportCDDialogProps> = (props) => {
  const { visible, info, selectItems, onClose } = props;
  const [form] = Form.useForm();

  useEffect(() => {
    if (visible) {
      //
    }
  }, [visible]);

  const onCancel = () => {
    onClose();
  };

  /**
   * @description: 保存表单方法
   */
  const onFinish = (vals: any) => {
    if (selectItems.length <= 0) {
      message.info('请选择需要导出的条目');
      return false;
    }
    const params: ICreateExportParams = jsonClone(vals);
    params.exportType = EnumReceiveTypes.SIP;
    params.fondsId = info.fondId;
    params.archiveTypeId = info.archiveTypeId;
    params.businessType = EnumExportBusType.Export;
    params.exportEntryInfo = {
      atCode: info.atCode,
      atType: info.atType,
      level: info.level,
      entryIdList: selectItems,
      searchCondition: '',
      dbName: info.dbName
    };

    createExportItem(params).then(() => {
      message.success('导出任务创建成功');
      onCancel();
    });
  };

  return (
    <Modal
      title="导出SIP包"
      visible={visible}
      className="create-sip-dialog"
      width={600}
      maskClosable={false}
      destroyOnClose
      onCancel={onCancel}
      footer={[
        <Button key="close" onClick={onCancel}>
          关闭
        </Button>,
        <Button
          key="export"
          type="primary"
          onClick={() => {
            form.submit();
          }}
        >
          导出
        </Button>
      ]}
    >
      <Form
        form={form}
        name="FolderForm"
        labelCol={{ span: 7 }}
        wrapperCol={{ span: 14 }}
        autoComplete="off"
        onFinish={onFinish}
      >
        <Form.Item label="已选择条目">{selectItems.length}条</Form.Item>
        <Form.Item name="name" label="导出名称">
          <Input placeholder="请填写离线生成的名字" />
        </Form.Item>
        <Form.Item
          name="templateId"
          label="导出字段配置"
          extra="请选择档案导出条件模板，规则模板可在“模板设计”内配置)"
          rules={[{ required: true, message: '请选择模板' }]}
        >
          <TransferTemplate tplType={EnumTemplateTypes.SipExport} />
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default ExportSipDialog;
